home *** CD-ROM | disk | FTP | other *** search
/ Internet Pratica / IPRAT_01.iso / ASP / ASPapp Portal / i_utils.asp < prev    next >
Text File  |  2002-03-12  |  14KB  |  441 lines

  1. <%
  2.  
  3. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  4. ':::::: i_utils.asp global function library for aspapp.com  :::::::::
  5. ':::::: copyright 1999-2001 Iatek,LLC. All rights reserved.  ::::::::
  6. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  7.  
  8.  
  9. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  10. ''  GLOBAL DECLARATIONS AND DATABASE CONNECTIONS
  11. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  12.  
  13. ''' initiate global vars and constants
  14. dim action
  15. dim b_error, a_errors, error_list, a_msg, msg_list
  16. dim cn, cmd, rs, rsselect, sql, do_search, a_records
  17.  
  18. ''' instantiate error handling and messaging
  19. set error_list = CreateObject("Scripting.Dictionary")
  20. set msg_list = CreateObject("Scripting.Dictionary")
  21.     
  22. ''' initiate db objects and connections
  23.  
  24. ''''' app database
  25. set cn = Server.CreateObject("ADODB.Connection")
  26. cn.Open "provider=microsoft.jet.oledb.4.0;data source=" & server.MapPath("data\7045.mdb") & ""
  27.  
  28. ''''' user database (may be the same as app)
  29. set user_cn = Server.CreateObject("ADODB.Connection")
  30. user_cn.Open "provider=microsoft.jet.oledb.4.0;data source=" & server.MapPath("data\7045.mdb") & ""
  31.  
  32. ''''' command object
  33. set cmd = Server.CreateObject("ADODB.Command")
  34. cmd.ActiveConnection = cn
  35.  
  36. ''''' recordset object
  37. set rs = Server.CreateObject("ADODB.Recordset")
  38.  
  39.  
  40.  
  41.  
  42. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  43. ''  ERROR AND MESSAGE DISPLAY SUBS
  44. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  45.  
  46. sub display_errs
  47. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  48.     ' display content of the error dictionary object
  49. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  50. if error_list.count > 0 then
  51.     ''' display errors
  52.     response.write "<div>"
  53.     a_errors = error_list.items
  54.     for i = 0 to error_list.count - 1
  55.     response.write "<li class=ErrFont>" & a_errors(i) & "</li>"
  56.     response.write "</div>"
  57.     next
  58. end if
  59. end sub
  60.  
  61. sub display_msg
  62. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  63. ' displays msgs after successful database action
  64. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  65.     ':: check if a msg was passed to the page
  66.     if request("msg") <> "" then msg_list.add "msg", request("msg")
  67.     ':: display messages
  68.     a_msg = msg_list.items
  69.     for i = 0 to msg_list.count - 1
  70.         response.write "<div class=MsgFont>" & a_msg(i) & "</div>"
  71.     next
  72. end sub
  73.  
  74.  
  75.  
  76. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  77. ''  USER MANAGMENT FUNCTIONS
  78. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  79.  
  80. function check_security(iLevel)
  81. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  82. ' authenticates user and verifies access level
  83. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  84.     if session("user_id") = "" OR isNull(session("accesslevel")) then
  85.         response.redirect("login.asp?querystring=" & to_url(request.serverVariables("QUERY_STRING")) & "&ret_page=" & to_url(request.serverVariables("SCRIPT_NAME")))
  86.     elseif session("accesslevel") <> "" then
  87.         if cLng(session("accesslevel")) < cLng(iLevel) then response.redirect("login.asp?msg=You+do+not+have+permission+to+access+the+requested+page.&querystring=" & to_url(request.serverVariables("QUERY_STRING")) & "&ret_page=" & to_url(request.serverVariables("SCRIPT_NAME")))
  88.     else
  89.         user_id = session("user_id")
  90.         accesslevel = session("accesslevel")
  91.     end if
  92. end function
  93.  
  94. sub do_login
  95. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  96. ' autheticates user in db and creates session
  97. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  98.     user_name = request("user_name")
  99.        password = request("password")
  100.     
  101.     sql = "SELECT user_name, password FROM Users WHERE user_name = " & to_sql(user_name,"text") & " AND password = " & to_sql(password,"text") & ""
  102.     set rs = user_cn.Execute(sql)
  103.     if rs.EOF then
  104.         'login failed
  105.         error_list.add "login", "Login or password in incorrect."
  106.         b_error = true
  107.     else
  108.         'login and password passed
  109.         sql = "SELECT user_id, accesslevel FROM Users WHERE user_name = " & to_sql(user_name,"text") & " AND password = " & to_sql(password,"text") & ""
  110.         set rs = user_cn.Execute(sql)
  111.         
  112.         if rs.EOF then
  113.             'should never happen
  114.             error_list.add "login", "User does not exist."
  115.             b_error = true
  116.         else
  117.             'login user
  118.             session("user_id") = rs(0)
  119.             session("accesslevel") = rs(1)
  120.             'where to next?
  121.             querystring = request("querystring")
  122.             ret_page = request("ret_page")
  123.             if (ret_page <> request.serverVariables("SCRIPT_NAME")) AND (ret_page <> "") then
  124.                 'return to page that preceded login
  125.                 response.redirect(ret_page & "?" & querystring)
  126.             else
  127.                 'go home
  128.                 response.redirect("default.asp")
  129.             end if
  130.         end if
  131.     end if
  132.     rs.Close
  133.       
  134. end sub
  135.  
  136.  
  137.  
  138. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  139. ''  FORMATTING FUNCTIONS
  140. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  141.  
  142. function to_url(strValue)
  143. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  144. ' make passed paramters url friendly
  145. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  146.     if IsNull(strValue) then strValue = ""
  147.     to_url = Server.URLEncode(strValue)
  148. end function
  149.  
  150. function to_html(strValue)
  151. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  152. ' convert string to html
  153. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  154.     if IsNull(strValue) then strValue = ""
  155.     to_html = Server.HTMLEncode(strValue)
  156. end function
  157.  
  158. function to_sql(Value,DataType)
  159.     if Value = "" or isNull(Value) then
  160.         to_sql = "NULL"
  161.     elseif DataType <> "number" then
  162.         to_sql = "'" & Replace(Value, "'", "''") & "'"
  163.     else
  164.         to_sql = Value
  165.     end if
  166. end function
  167.  
  168. function get_options(sql,selected_value)
  169. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  170. ' displays option tags for a select list
  171. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  172.     'response.write sql
  173.     if isNull(selected_value) then selected_value = ""
  174.     set rsSelect = cn.Execute(sql)
  175.     do until rsSelect.EOF
  176.         if not isNull(rsSelect(0)) then
  177.             get_options = get_options + "<option"
  178.             if cStr(rsSelect(0)) = cStr(selected_value) then
  179.                 get_options = get_options + " SELECTED"
  180.             end if
  181.             get_options = get_options + " value='" & rsSelect(0) & "'>"
  182.             if rsSelect.Fields.Count-1 = 0 then
  183.                 get_options = get_options + "" & rsSelect(0) & " "
  184.             else
  185.                 for i = 1 to rsSelect.Fields.Count-1
  186.                     if rsSelect(i) <> "" then
  187.                         get_options = get_options + "" & rsSelect(i)
  188.                         if i < rsSelect.Fields.Count-1 then get_options = get_options + ": "
  189.                     end if
  190.                 next
  191.             end if
  192.             get_options = get_options + "</option>" & vbCRLF & chr(9) & chr(9)
  193.         end if
  194.     rsSelect.MoveNext
  195.     loop
  196.     rsSelect.Close
  197. end function
  198.  
  199. function is_reserved(strValue)
  200. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  201. ' compare a string with a list of vb and sql reserved words
  202. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  203.     reserved_words = "|and||as||boolean||byref||byte||byval||call||case||class||const||currency||date||desc||debug||dim||do||double||each||else||elseif||empty||end||endif||enum||eqv||event||exit||false||for||function||get||goto||if||imp||implements||in||integer||is||let||like||long||loop||lset||me||mod||new||next||not||nothing||null||on||option||optional||or||paramarray||preserve||private||public||raiseevent||redim||rem||resume||rows||rset||select||set||shared||single||size||static||stop||sub||then||to||true||type||typeof||until||variant||wend||while||with||xor|"
  204.     if inStr(reserved_words,"|" & lcase(strValue) & "|") > 0 then
  205.         is_reserved = true
  206.     else
  207.         is_reserved = false
  208.     end if
  209. end function
  210.  
  211.  
  212.  
  213. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  214. ''  GENERIC DATABASE SUBS -- These are handy, but not optimal for db reads and writes
  215. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  216.  
  217. function db_select(tablename,keyfield,keyvalue)
  218. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  219. ' selects a key record from db and stores fieldnames
  220. ' and values in the global a_records array (first element).
  221. ' The function will return 1 if values are found, otherwise 0.
  222. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  223.     
  224.     dim rsT
  225.     dim rsSQL
  226.     
  227.     rsSQL = "SELECT * FROM " & tablename & " WHERE " & keyfield & " = " & keyvalue
  228.     set rsT = cn.Execute(rsSQL)
  229.     
  230.     if not rsT.EOF then
  231.         db_select = 1
  232.         redim a_records(1,rsT.Fields.Count-1,1)
  233.         for i = 0 to (rsT.Fields.Count-1)
  234.             a_records(1,i,0) = rsT(i).name
  235.             a_records(1,i,1) = rsT(i)
  236.         next
  237.     else
  238.         db_select = 0
  239.     end if    
  240.     
  241.     rsT.close
  242.     set rsT = NOTHING
  243.  
  244. end function
  245.  
  246. function db_insert(tablename,keyfield)
  247. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  248. ' examines name and values in the .asp request object and
  249. ' creates an insert statement corresponding to the names
  250. ' and values found in the request object. Attemps to insert
  251. ' the record into tablename. The function will
  252. ' return the value of the keyfield for the newly inserted
  253. ' record, otherwise 0.
  254. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  255.  
  256.     dim rsT
  257.     dim rsSQL
  258.  
  259.     rsSQL = "SELECT TOP 1 * FROM " & tablename
  260.     set rsT = cn.Execute(rsSQL)
  261.     
  262.     if not rsT.EOF then
  263.         rsSQL = "INSERT INTO " & tablename
  264.         rsSQL = rsSQL + "("
  265.         
  266.         for i = 0 to (rsT.Fields.Count-1)
  267.             if (request(rsT(i).name) <> "") AND rsT(i).name <> keyfield then
  268.                 rsSQL = rsSQL + "" & rsT(i).name & ""
  269.                 if i <> rsT.Fields.Count-1 then rsSQL = rsSQL + ","
  270.             end if
  271.         next
  272.         
  273.         ''' truncate last comma
  274.         rsSQL = left(rsSQL,len(rsSQL)-1)
  275.         
  276.         rsSQL = rsSQL + ") VALUES ("
  277.         
  278.         for i = 0 to (rsT.Fields.Count-1)
  279.             if (request(rsT(i).name) <> "") AND rsT(i).name <> keyfield then
  280.                 value = request(rsT(i).name)
  281.                 ''' determine datatype
  282.                 ''' for more info http://www.aspdeveloper.net/iasdocs/aspdocs/ref/comp/daprop06_4.htm
  283.                 select case rsT(i).type
  284.                 case 129,7,133,134,135,205,201,203,204,200,128
  285.                     rsSQL = rsSQL + "" & to_sql(value,"text") & ","
  286.                 case else
  287.                     rsSQL = rsSQL + "" & to_sql(value,"number") & ","
  288.                 end select
  289.             end if
  290.         next
  291.         
  292.         ''' truncate last comma
  293.         rsSQL = left(rsSQL,len(rsSQL)-1)
  294.         
  295.         rsSQL = rsSQL + ")"
  296.         response.write rsSQL
  297.         'on error resume next
  298.         cn.Execute(rsSQL)
  299.         if err.Number <> 0 then
  300.             b_error = true
  301.             error_list.add "db_insert_" & err.Number ,"The insert failed: " & tablename & "." & err.Description
  302.             db_insert = 0
  303.         else                
  304.             set rsT = cn.Execute("SELECT @@IDENTITY")
  305.             db_insert = rsT(0)
  306.         end if
  307.         on error goto 0
  308.         
  309.     else
  310.         db_insert = 0
  311.     end if    
  312.     
  313.     rsT.close
  314.     set rsT = NOTHING
  315.  
  316. end function
  317.  
  318. function db_update(tablename,keyfield)
  319. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  320. ' examines name and values in the .asp request object and
  321. ' creates an update statement corresponding to the names
  322. ' and values found in the request object. Attemps to
  323. ' update the record in tablename. If successful, the
  324. ' function will the return the value of 1, otherwise 0.
  325. ' The value of the keyfield also must be contained in the
  326. ' request object.
  327. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  328.  
  329.     dim rsT
  330.     dim rsSQL
  331.  
  332.     rsSQL = "SELECT TOP 1 * FROM " & tablename
  333.     set rsT = cn.Execute(rsSQL)
  334.     
  335.     if not rsT.EOF and request(keyfield) <> "" then
  336.         rsSQL = "UPDATE " & tablename
  337.         rsSQL = rsSQL + " SET "
  338.         
  339.         for i = 0 to (rsT.Fields.Count-1)
  340.             if (request(rsT(i).name) <> "") AND rsT(i).name <> keyfield then
  341.                 name = rsT(i).name
  342.                 value = request(rsT(i).name)
  343.                 ''' determine datatype
  344.                 ''' for more info http://www.aspdeveloper.net/iasdocs/aspdocs/ref/comp/daprop06_4.htm
  345.                 select case rsT(i).type
  346.                 case 129,7,133,134,135,205,201,203,204,200,128
  347.                     rsSQL = rsSQL + "" & name & " = " & to_sql(value,"text") & ","
  348.                 case else
  349.                     rsSQL = rsSQL + "" & name & " = " & to_sql(value,"number") & ","
  350.                 end select
  351.             end if
  352.         next
  353.         
  354.         ''' truncate last comma
  355.         rsSQL = left(rsSQL,len(rsSQL)-1)
  356.         
  357.         rsSQL = rsSQL + " WHERE " & keyfield & " = " & request(keyfield)
  358.         
  359.         'response.write rsSQL
  360.         on error resume next
  361.         cn.Execute(rsSQL)
  362.         if err.Number <> 0 then
  363.             b_error = true
  364.             error_list.add "db_update_" & err.Number ,"The update failed: " & tablename & "." & err.Description
  365.             db_update = 0
  366.         else            
  367.             db_update = 1
  368.         end if
  369.         on error goto 0
  370.         
  371.     else
  372.         db_update = 0
  373.     end if    
  374.     
  375.     rsT.close
  376.     set rsT = NOTHING
  377.  
  378. end function
  379.  
  380. function db_query(sql)
  381. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  382. ' selects record(s) from db and stores fieldnames
  383. ' and values in the global a_records array. The function
  384. ' will return 1 if values are found, otherwise 0.
  385. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  386.  
  387.     cmd.CommandText = sql
  388.     set rsT = Server.CreateObject("ADODB.Recordset")
  389.     rsT.CursorLocation = 3
  390.     rsT.Open cmd
  391.         
  392.     if not rsT.EOF then
  393.         db_query = 1
  394.         num_records = rsT.RecordCount
  395.         redim a_records(num_records-1,rsT.Fields.Count-1,1)
  396.         do until rsT.EOF
  397.             for j = 0 to (rsT.Fields.Count-1)
  398.                 a_records(i,j,0) = rsT(j).name
  399.                 a_records(i,j,1) = rsT(j)
  400.             next
  401.         rsT.MoveNext
  402.         i = i + 1
  403.         loop
  404.     else
  405.         db_query = 0
  406.     end if    
  407.     
  408.     rsT.close
  409.     set rsT = NOTHING
  410.  
  411. end function
  412.  
  413.  
  414.  
  415. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  416. ''  TREE FORM FUNCTIONS
  417. '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  418.  
  419. sub clearTree
  420. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  421. ' clears array used to construct tree forms
  422. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  423.     redim aTree(0)
  424.     aTree(0) = ""
  425. end sub
  426.  
  427. sub addItem(sCurrTree, sCurrTreeIMAGE, sTitle, sAnchor, sTarget)
  428. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  429. ' adds an item to the tree array
  430. ':::::::::::::::::::::::::::::::::::::::::::::::::::::
  431.     dim BRK
  432.     BRK = "||"
  433.  
  434.     aTree(uBound(aTree)) = sCurrTree & BRK & sCurrTreeIMAGE & BRK & sTitle & BRK & sAnchor & BRK & sTarget
  435.  
  436.     redim preserve aTree(uBound(aTree) + 1)
  437.  
  438. end sub
  439.  
  440. %>
  441.